package br.edu.JUniversity.controllers.documento.actions;

import java.util.List;





import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;







import br.edu.JUniversity.dao.DocumentoDAO;
import br.edu.JUniversity.model.Documento;
import br.edu.JUniversity.model.TipoDocumento;

@Namespace(value = "/documento")
public class DocumentoAction {

	private List<TipoDocumento> ListTipoDocumentos;
	private List<Documento> documentos;
	private DocumentoDAO dao;
	private Long id;
	private Documento documento;
	
	public DocumentoAction() {
		dao = new DocumentoDAO();
		list();
	}

	@Action(value = "list", results = { @Result(name = "ok", location = "/DocumentoList.jsp") })
	public String list() {
		documentos = dao.lista();
		
		return "ok";
	}

	@Action(value = "form", results = { @Result(name = "ok", location = "/AddDocumento.jsp") })
	public String form() {
		
		return "ok";
	}

	@Action(value = "edit", results = { @Result(name = "ok", location = "/EditDocumento.jsp") })
	public String edit() {
		documento = new DocumentoDAO().buscaPorId(id);
		return "ok";
	}

	@Action(value = "update", results = { @Result(name = "ok", type = "redirectAction", params = {
			"actionName", "list" }) })
	public String update() {
		dao.altera(documento);
		return "ok";
	}
	
	@Action(value = "insert", results = {
			@Result(name = "ok", type = "redirect", location = "list"),
			@Result(name = "input", location = "/AddDocumento.jsp")

	})
	public String insert() {
		
		new DocumentoDAO().adiciona(documento);
		return "ok";
		
	}
	
	@Action(value = "delete", results = { @Result(name = "ok", type = "redirectAction", params = {
			"actionName", "list" })

	})
	public String delete() {
		new DocumentoDAO().remove(documento);
		try {
			Thread.sleep(1000);
		}catch(Exception e){}
		return "ok";
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Documento getDocumento() {
		return documento;
	}

	public List<Documento> getDocumentos() {
		return documentos;
	}
	

	public void setDocumento(Documento documento) {
		this.documento = documento;
	}
	
	public List<TipoDocumento> ListTipoDocumentos() {
		return ListTipoDocumentos;
	}

}
